﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Telerik.Web.Mvc;

namespace Presentation.Web.Admin.Controllers
{
    [HandleError]
    public class LoggerController : Infrastructure.Crosscutting.NetFramework.Controllers.BaseController
    {
        Domain.Uow.IRepository<Domain.Entity.Log> logRepository;

        public LoggerController(Infrastructure.Crosscutting.NetFramework.IWorkContext work,
            Domain.Uow.IRepository<Domain.Entity.Log> log)
            : base(work)
        {
            if (log == null)
            {
                throw new ArgumentNullException("logRepository is null");
            }
            logRepository = log;
        }

        #region log

        public ViewResult Index()
        {
            var data = this.GetLogTrueState(1, 10).ToDto();
            ViewBag.total = this.GetLogTrueState().Count();
            return View(data);
        }

        [GridAction(EnableCustomBinding = true)]
        [HttpPost]
        public JsonResult AjaxIndex(GridCommand comm)
        {
            var data = this.GetLogTrueState(comm.Page, comm.PageSize).ToDto();
            var model = new GridModel { Data = data, Total = this.GetLogTrueState().Count() };
            return Json(model, JsonRequestBehavior.AllowGet);
        }

        [GridAction(EnableCustomBinding = true)]
        [HttpPost]
        public JsonResult Delete(int id, GridCommand comm)
        {
            var entity = logRepository.Get(id);
            if (entity != null)
            {
                logRepository.Delete(entity);
                logRepository.Uow.Commit();
            }
            var data = this.GetLogTrueState(comm.Page, comm.PageSize);
            return Json(new { data = data.ToDto(), total = this.GetLogTrueState().Count() }, JsonRequestBehavior.AllowGet);
        }


        [HttpPost]
        public JsonResult MulDelete(FormCollection del)
        {
            var res = false;
            var message = "";
            IQueryable<Domain.Dto.LogDto> data = null;
            var selectedList = del.GetValues("selectRow");
            if (selectedList != null && selectedList.Count() > 0)
            {
                var idList = selectedList.ToIntList();
                var entity = logRepository.GetList().Where(e => idList.Contains(e.Id));
                foreach (var item in entity)
                {
                    logRepository.Delete(item);
                }
                try
                {
                    logRepository.Uow.Commit();
                    res = true;
                    var piStr = Request["pi"];
                    var takeStr = Request["take"];
                    var pi = 0;
                    var take = 10;
                    int.TryParse(piStr, out pi);
                    int.TryParse(takeStr, out take);
                    data = this.GetLogTrueState(pi, take).ToDto().AsQueryable();
                }
                catch (Exception ex)
                {
                    message = ex.Message;
                }
            }
            //返回空，是为了不再请求数据，因为他自己会重新binding的
            return Json(new { success = res, message = message, data = data }, JsonRequestBehavior.AllowGet);
        }

        [HttpPost]
        public JsonResult ClearAll()
        {
            var res = false;
            var message = "";
            var all = logRepository.GetList();
            foreach (var item in all)
            {
                logRepository.Delete(item);
            }
            try
            {
                logRepository.Uow.Commit();
                res = true;
            }
            catch (Exception ex)
            {
                message = ex.Message;
            }
            return Json(new { success = res, message = message });
        }


        #endregion

        #region util

        protected IQueryable<Domain.Entity.Log> GetLogTrueState(int pi, int take)
        {
            pi = pi - 1 > 0 ? pi - 1 : 0;
            var data = logRepository.GetList().OrderByDescending(e => e.Id)
                .Skip(pi * take)
                .Take(take);
            return data;
        }

        protected IQueryable<Domain.Entity.Log> GetLogTrueState()
        {
            var data = logRepository.GetList().OrderByDescending(e => e.Id);
            return data;
        }

        #endregion
    }
}
